﻿package com.hongguan.oms.service.grid;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.stereotype.Service;

import com.hongguan.oms.domain.grid.GridInfo;
import com.hongguan.oms.domain.grid.GridInfoExample;
import com.hongguan.oms.service.BaseExampleService;

/**
 * GridInfo Service
 * 
 * @author 
 * 
 */
@Service
public class GridInfoService extends BaseExampleService<GridInfo, GridInfoExample> {

	public GridInfoService() {
		super();
		namespace = "GRID_INFO";
	}
	
	@Autowired
	protected SqlMapClientTemplate sqlMapClientTemplate;
	public SqlMapClientTemplate getSqlMapClientTemplate() {
		return this.sqlMapClientTemplate;
	}
	public boolean isNameSame(GridInfo gridinfo){
		boolean flag=false;
		long num=(Long)this.getSqlMapClientTemplate().queryForObject("GRID_INFO.is_grid_name_same",gridinfo);
		if(num>0){
			flag=true;
		}
		return flag;
	}
	/**
	 * 保存GridInfo
	 * 
	 * @param gridinfo
	 * 
	 */
	public void saveGridInfo(GridInfo gridinfo) {
		if (gridinfo.getGridId() == null) {
			 insert(gridinfo);
		} else {
			updateByPrimaryKey(gridinfo);
		}
	}
	
	//保存网格返回ID
	public Long saveOrUpdateGridInfo(GridInfo gridinfo){
		if(gridinfo.getGridId() == null || gridinfo.getGridId() <= 0)
			return (Long)getSqlMapClientTemplate().insert("GRID_INFO.insert", gridinfo);
		else{
			getSqlMapClientTemplate().update("GRID_INFO.updateByPrimaryKey", gridinfo);
			return gridinfo.getGridId();
		}
		
	}

	/**
	 * 删除GridInfo
	 * 
	 * @param gridinfo
	 *            　GridInfo
	 */
	public void deleteGridInfo(GridInfo gridinfo) {
		deleteByPrimaryKey(gridinfo);
	}
	
	/**
	 * 删除267与269层点与网格的关系
	 * @param id
	 */
	public void deletePointByGridId(Long id){
		getSqlMapClientTemplate().delete("GRID_INFO.deleteGridPointRelByGridId", id);
	}
	
	/**
	 * 插入267与269层点与网格的关系
	 */
	public void insertPointAndGrid(Long gridId,Long pointId){
		Map<String,Long> map = new HashMap<String,Long>();
		map.put("gridId", gridId);
		map.put("pointId", pointId);
		getSqlMapClientTemplate().insert("GRID_INFO.insertGridPointRel", map);
		
	}
	public List queryGridByAreaId(Map param){
		return this.getSqlMapClientTemplate().queryForList("GRID_INFO.queryGridByAreaId",param);
	}
}

